package me.littlesquid.bettertp;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;

import java.io.File;
import java.io.IOException;

public class FileManager {
    public static void reloadfile(FileConfiguration configuration, File file){
        try {
            configuration.save(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            configuration.load(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InvalidConfigurationException e) {
            throw new RuntimeException(e);
        }
    }
    public static void savecurrentlocation(Location location ,FileConfiguration data,String name){
        String world=location.getWorld().getName();
        int x=location.getBlockX();
        int y= location.getBlockY();
        int z= location.getBlockZ();
        data.set(name+".current",world+";"+x+";"+y+";"+z);
    }
    public static void savedeathlocation(Location location ,FileConfiguration data,String name){
        String world=location.getWorld().getName();
        int x=location.getBlockX();
        int y= location.getBlockY();
        int z= location.getBlockZ();
        data.set(name+".death",world+";"+x+";"+y+";"+z);
    }
    public static Location loadlocation(String loc){
        String[]list=loc.split("\\;");
        World world= Bukkit.getWorld(list[0]);
        int x= Integer.parseInt(list[1]);
        int y= Integer.parseInt(list[2]);
        int z= Integer.parseInt(list[3]);
        Location location=new Location(world,x,y,z);
        return location;
    }
}
